# 引入必要的东西
import pandas as pd
import requests
import json
key_li = "930d28747b313ddc559a5c729d292aab"

# 地理编码
def geocode(address,city=None,batch=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': key_li,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig,
    }
    response = requests.get(url='https://restapi.amap.com/v3/geocode/geo?parameters',params=params)
    data = response.json()
    return data
中大南方 = geocode(address='广东省广州市中山大学南方学院')
print(中大南方)
中大南方地理编码 = 中大南方['geocodes'][0]['location']
print(中大南方地理编码)

# 逆地理编码
def regeocode(location,poitype=None,radius=None,extensions="base",batch=False,roadlevel=None,sig=None,homeorcorp=None)->dict:
    """获取逆地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key': key_li,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'roadlevel':roadlevel,
        'homeorcorp':homeorcorp,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

中大南方逆地理编码 = regeocode(中大南方地理编码)
print(中大南方逆地理编码)
df_中大南方逆地理编码 = pd.json_normalize(中大南方逆地理编码).T
display(df_中大南方逆地理编码)

# 步行路径规划
def walking(origin,destination,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key_li,
        'origin':origin,
        'destination':destination,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

天河城 = geocode('广东省广州市天河区天河城')
天河城_location = 天河城['geocodes'][0]['location']
中大南方_location = 中大南方['geocodes'][0]['location']
print("(起点)中大南方_location:",中大南方_location,"(终点)天河城_location:",天河城_location)

中大南方_天河城 = walking(中大南方_location,天河城_location)
df_步行路径规划 = pd.json_normalize(中大南方_天河城["route"]["paths"][0]['steps'])
print(中大南方_天河城)
display(df_步行路径规划)
df_步行路径规划["instruction"]

# 公交路径规划
def integrated(origin,destination,city,cityd=None,extensions='base',strategy=None,nightflag=0,date=None,time=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params={
        'key':key_li,
        'origin':origin,
        'destination':destination,
        'city':city,
        'cityd':cityd,
        'extensions':extensions,
        'strategy':strategy,
        'nightflag':nightflag,
        'date':date,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

中山大学 = geocode('广东省广州市中山大学南校区')
中山大学_location = 中山大学['geocodes'][0]['location']
正佳广场 = geocode('广东省广州市天河区正佳广场')
正佳广场_location = 正佳广场['geocodes'][0]['location']
print("(起点)中山大学_location:",中山大学_location,"(终点)正佳广场_location:",正佳广场_location)

中大_正佳广场 = integrated(中山大学_location,正佳广场_location,city='广州',extensions='all')
print(中大_正佳广场)
df_bus = pd.json_normalize(中大_正佳广场)
df_bus

# 行政区域查询
def district(keywords,subdistrict=None,page=None,offset=None,extensions='base',filter=None,)->dict:
    url = 'https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key_li,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'page':page,
        'offset':offset,
        'extensions':extensions,
        'filter':filter,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
行政区域 = district(keywords='广州市',extensions='base',subdistrict=3)
行政区域
df_行政区域_1级 = pd.json_normalize(行政区域["districts"][0]['districts'])
df_行政区域_1级
df_行政区域_2级_广州市 = pd.json_normalize(行政区域["districts"][0]['districts'][3]['districts'])
df_行政区域_2级_广州市

# 搜索POI
def place_text(keywords,types,city=None,citylimit=None,children=None,page=None,extensions='base',sig=None)->dict:
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params={
        'key':key_li,
        'keywords':keywords,
        'types':types,
        'city':city,
        'citylimit':citylimit,
        'children':children,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

广州_中学 = place_text(keywords=None,types='中学',city="广州市",children=1,extensions='all',page=3)
print(广州_中学)
df_广州_中学 = pd.json_normalize(广州_中学["pois"])
df_广州_中学

# 静态地图
from PIL import Image
from io import BytesIO
def staticmap(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,page=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/staticmap?parameters'
    params={
        'key':key_li,
        'location':location,
        'zoom':zoom,
        'size':size,
        'scale':scale,
        'markers':markers,
        'labels':labels,
        'paths':paths,
        'traffic':traffic,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = Image.open(BytesIO(response.content))
    return data

# IP定位
def ip(key,ip,sig=None)->dict:
    url = "https://restapi.amap.com/v3/ip?parameters"
    params={
        'key':key_li,
        'ip':ip,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    result = response.json()
    return result

# 坐标转换
def convert(key,location,coordsys="autonavi",sig=None,output="json")->dict:
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        'key':key_li,
        "locations": location,
        "coordsys": coordsys,
        "sig": sig,
        "output": output
          }
    response = requests.get(url,params)
    return response.json()

# 天气查询
def weather(key,city,extension="all",output="json"):
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key": key_li,
        "city": city,
        "extensions":extension,
        "output": output
    }
    r = requests.get(url,params)
    return r.json()

# 输入提示
def input_tips(key,keyword,poi_type=None,location=None,city=None,citylimit="false",datatype="all",sig=None,output="json",callback=None):
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key": key_li,
        "keywords": keyword,
        "type": poi_type,
        "location": location,
        "city": city,
        "citylimit": citylimit,
        "datatype": datatype,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(url,params)
    return r.json()